import {Component, OnInit} from '@angular/core';
import {AddGooBrandComponent} from '../add-goo-brand.component';
import {ErrRes} from '../../../../../frame/global/err-res';
import {GooBrand} from '../../goo-brand.type';
import {Services} from '../../../../../frame/module/services';
import {HttpRequestClient} from '../../../../../frame/http-request-client/http-request-client';
import {UrlRou} from '../../../../../frame';
import {Zero3Alert} from '../../../../../frame/global/zero3-alert/zero3-alert.component';

@Component({
    selector: 'app-main-add-goo-brand',
    templateUrl: './main-add-goo-brand.component.html',
    styleUrls: ['./main-add-goo-brand.component.css']
})
export class MainAddGooBrandComponent implements OnInit {

    errRes = new ErrRes();

    domain = new GooBrand();

    extend = {
        domain: {
            default: () => {
                this.domain.serAudit = 'WAIT_SUBMIT';
                this.domain.serUsing = 'ENABLE';
            },
            serAudit: () => {
                return 'serAudit';
            },
            file: {
                oss: Services.aliOSS.simple(),
                change: (event) => {
                    console.log(event.target.value); // file name
                    this.extend.domain.file.oss.uploadFile((response, status, headers) => {
                        if (status === 200) {
                            this.extend.domain.file.oss.fileUrl = JSON.parse(response).detail.fileUrl;
                            this.domain.serBarndPricture = this.extend.domain.file.oss.fileUrl;
                        } else {
                            this.extend.domain.file.oss.fileUrl = '';
                            this.domain.serBarndPricture = this.extend.domain.file.oss.fileUrl;
                            Zero3Alert.req('fail');
                        }
                    });
                }
            }
        },
    };


    func = {
        save: {
            req: (event) => {
                Services.gooBrand.save(this.domain, this.func.save.callback).subscribe();
            },
            callback: {
                success: (result) => {
                    HttpRequestClient.router.navigate([UrlRou.gooBrand.list]);
                },
                error: (result) => {
                    this.errRes.error = result.error;
                    this.errRes.statusText = result.statusText;
                }
            }
        },
        back: {
            req: (event) => {
                HttpRequestClient.router.navigate([UrlRou.gooBrand.list]);
                console.log('fun back');
            }
        }
    };

    constructor(public addGooBrand: AddGooBrandComponent) {
        this.addGooBrand.main = this;
    }

    ngOnInit() {
        this.extend.domain.default();
    }

}
